package com.study.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.{SparkConf, SparkContext}

object Scala18_RDD_Operator_Transform{
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    conf.set("spark.local.dir","D:\\hadoopbook\\spark\\test")
    val sc = new SparkContext(conf)
    // TODO 算子 - 转换 -  combineByKey
    // TODO 将数据List(("a", 88), ("b", 95), ("a", 91), ("b", 93), ("a", 95), ("b", 98))求每个key的平均值
    val rdd = sc.makeRDD(
      List(
        ("a", 1), ("a", 2), ("b", 3),
        ("b", 4), ("b", 5), ("a", 6)
      ), 2
    )
    // TODO　combineByKey算子也可以实现WorldCount 6
    val rdd1 = rdd.combineByKey(
      num => num,
      (x: Int, y : Int) => {
        x+y
      },
      (x: Int, y: Int) => {
        x + y
      }
    )
    rdd1.collect().foreach(println)
    /*
    (b,12)
    (a,9)
     */
    sc.stop()

  }

}
